var WASP_INSTITUTION = {
	formatViewHtml: function(text, options, rData) {
		var instLName = rData["instLName"];
        var instId = rData["instId"];
        if(null!=instLName){
            return '<a href="#" class="showunderline" onclick="WASP_INSTITUTION.institutionDetailView(\''+instId+'\');" >' + text + '</a>';
        }else{
        	return "";
        }    
    },
    institutionAddView: function() {
        /** 新增机构信息 */
    	 var actionUrl = PATH_INSTITUTION+"institutionAddView.do";
		 window.open(actionUrl);
    },
    institutionUpdateView: function(instId) {
	   /** 更新机构信息 */
		 var actionUrl = PATH_INSTITUTION+"institutionUpdateView.do?instId="+instId;
		 openDialog(actionUrl);
	},
    
	institutionDetailView: function(instId) {
        /** 查看机构信息 */
    	 var actionUrl = PATH_INSTITUTION+"institutionDetailView.do?instId="+instId;
    	 openDialog(actionUrl);
    },
    deleteInstitution: function(instId) {
    	ctools.confirm("是否确认删除该变更记录？",function(){
    		$.ajax({
        		url: PATH_INSTITUTION + "logicDeleteInstitutionInfoById.do",
        		data: {"instId": instId},
        		dataType: "JSON",
        		type: "POST",
        		success: function (data) {
        			if (data.success) {
        				swal("操作成功!", "", "success");
        				queryByCondtion(false);
        			}
        		},
        		error: function (xhr) {
        			sweetAlertAjaxError(xhr);
        		}
        	});
    	});
    }
};

/**
 *@from institutionList.html
 **/
var $institutionList = $('#institutionList');
$(function() {
	//注册清空事件
    WASP_WIDGET.registerResetClearEvent();
    $('#instType').select2({ placeholder: '机构类型'});
    $('#instRolType').select2({ placeholder: '角色'});
    $('#status').select2({ placeholder: '状态'});
	
    $institutionList.jqGrid({
        url: PATH_INSTITUTION +'institutionInfoListPage.do',
        caption: '机构信息列表<button type="button" class="btn btn-default btn-sm btn-small btn-fullscreen" title="" tabindex="-1"><i class="fa fa-arrows-alt icon-fullscreen"></i></button>',
        datatype: "json",
        colNames: [
	       	       '机构ID', '机构简称', '机构全称', 
	       	       '机构类型', '父级机构','角色','状态id','状态','修改人','创建人','复核人', '操作'
	        	  ],    
        colModel: [
            {name:'instId',index:'instId',align:'left',key:true, resizable:true},
			{name:'instSName',index:'instSName',align:'left',sortable:false, resizable:true},
			{name:'instLName',index:'instLName',align:'left',sortable:false, resizable:true},
			{name:'instTypeNm',index:'instTypeNm',align:'left',sortable:false, resizable:true},
			{name:'instParentIdNm',index:'instParentIdNm',align:'left',sortable:false, resizable:true},
			{name:'instRolTypeNm',index:'instRolTypeNm',align:'left',sortable:false, resizable:true},
			{name:'status',index:'status',align:'left',sortable:false, resizable:true,hidden:true},
			{name:'statusName',index:'statusName',align:'left',sortable:false, resizable:true},
			{name:'modifyId',index:'modifyId',align:'left',sortable:false, resizable:true,hidden:true},
			{name:'createId',index:'createId',align:'left',sortable:false, resizable:true,hidden:true},
			{name:'checkId',index:'checkId',align:'left',sortable:false, resizable:true,hidden:true},
            {name: 'option', index: 'option', resizable: true, align:'left',sortable: false }
        ],
        rowNum: 20,
        rowList: [20, 30, 50],
        rownumbers: true,
        rownumWidth: 50,
        prmNames: {
        	        search: "search", 
        	        page: "pageNo",
        	        rows: "limit" 
        	       },
        height: 'auto',
        width: false,
        editurl: '',
        viewrecords: true,
        cellEdit: false,
        shrinkToFit: true,
        grouping: false,
        jsonReader: {
            root: "items", //结果集
            records: "total", //总记录数 
            total: "pageCount", //总页数
            page: "pageNo", //当前页 
            repeatitems: false // (4) 
        },
        pager: "#institutionPage",
        viewrecords: true,
        hidegrid: false,
        //这里是隔行变色
        // altRows: true,
        // altclass: 'tr-striped',
        gridComplete: function() {
            var ids = $institutionList.jqGrid('getDataIDs');
            for (var i = 0; i < ids.length; i++) {
                var instId = ids[i];
            	var del = '<a date-rowid="' + instId + '" href="javascript:;" class="btn btn-link btn-jqgrid" id="'+ instId +'Active" name="'+ instId +'Active" title="删除" onclick="WASP_INSTITUTION.deleteInstitution(\''+instId+'\');" >删除</a>';
            	$institutionList.jqGrid('setRowData',instId,{option: del});
            }
        }
    });
    $institutionList.navGrid('#institutionPage', { edit: false, add: false, del: false, search: false, refreshstate: 'current' });
    $institutionList.jqGrid('setFrozenColumns');
    jqGridResize($institutionList);

});


function queryByCondtion(flag, tab){
	
	var instLName = $.trim($("#instLName").val());   
    var instType = $.trim($("#instType").val());  
    var instRolType = $.trim($("#instRolType").val());  
    
    var postData = $institutionList.jqGrid("getGridParam", "postData");
    $.extend(postData, {
    	'sp[instLName]':instLName,
        'sp[instType]':instType,
        'sp[instRolType]':instRolType
    });
    if (flag) {
    	$institutionList.jqGrid("setGridParam",{"datatype": "json"}).trigger("reloadGrid", [{ page: 1 }]); //重新载入Grid表格
    } else {
    	$institutionList.jqGrid("setGridParam",{"datatype": "json"}).trigger("reloadGrid"); //重新载入Grid表格
    }
}

//对按钮绑定全屏事仿
$(document).on('click', '.btn-fullscreen', function(){
    var $wrapper = $(this).parents('.fullscreen-wrapper')[0];
    toggleFullScreen(document.documentElement);
    //全屏的时候将几个模瀦F放到下面去
    $('.modal[role="dialog"]').appendTo($wrapper);
});

